package com.zl.dao;

import java.sql.Connection;
import java.sql.ResultSet;

import com.mysql.jdbc.PreparedStatement;
import com.zl.model.Message;

public class MessageDao {

	/**
	 * 新增留言信息
	 * @param connection
	 * @param message
	 * @return
	 * @throws Exception
	 */
	public static int add(Connection connection, Message message) throws Exception {
		
		String sql = "insert into t_message (userId, userName, messageContent, messageTime) values(?, ?, ?, ?)";
		
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		statement.setInt(1, message.getUserId());
		statement.setString(2, message.getUserName());
		statement.setString(3, message.getMessageContent());
		statement.setString(4, message.getMessageTime());
		
		return statement.executeUpdate();
	}
	
	/**
	 * 查询用户留言
	 * @param connection
	 * @param message
	 * @return
	 * @throws Exception
	 */
	public static ResultSet select(Connection connection) throws Exception {
		
		String sql = "select * from t_message order by messageTime Desc";
		
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		
		return statement.executeQuery();
	}
	
}
